﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.templates.label
{
    public partial class label_htmledit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string LabelName = ChangeHope.WebPage.PageRequest.GetQueryString("name");
                string ModuleId = ChangeHope.WebPage.PageRequest.GetQueryString("moduleid");
                //标签类型
                string LabelClass = ChangeHope.WebPage.PageRequest.GetQueryString("LT");

                if (LabelName != "")
                {
                    bindInfo(LabelName, LabelClass);
                    ViewState["LabelName"] = LabelName;
                }
                string FileName = ChangeHope.WebPage.PageRequest.GetQueryString("FileName");
                string FilePath = ChangeHope.WebPage.PageRequest.GetQueryString("FilePath");
                this.hlBack.NavigateUrl = "../amendlable.aspx?dir=~\\" + FilePath + "&filename=" + FileName + "";
                this.hfBottomBack.NavigateUrl = "../amendlable.aspx?dir=~\\" + FilePath + "&filename=" + FileName + "";
            }
        }

        #region 绑定类型
        /// <summary>
        /// 绑定类型
        /// </summary>
        private void bindType(bool judge)
        {
            YXShop.BLL.Templates.LabelType data = new YXShop.BLL.Templates.LabelType();
            ChangeHope.DataBase.DataByPage dataPage = data.GetList();
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    if (judge == true)
                    {
                        if (dataPage.DataReader["id"].ToString() != "1")
                        {
                            this.ddlLabelClass.Items.Add(new ListItem(dataPage.DataReader["name"].ToString(), dataPage.DataReader["id"].ToString()));
                        }
                    }
                    else
                    {
                        this.ddlLabelClass.Items.Add(new ListItem(dataPage.DataReader["name"].ToString(), dataPage.DataReader["id"].ToString()));
                    }
                }
            }
            dataPage.Dispose();
            dataPage = null;
        }
        #endregion

        #region 绑定信息
        protected void bindInfo(string name, string labelclass)
        {
            YXShop.BLL.Templates.Label bll = new YXShop.BLL.Templates.Label();
            YXShop.Model.Templates.Label model = bll.GetModelName(name);
            if (model != null)
            {
                bindType(false);
                if (model.TID == 1)
                {
                    this.btnOK.Enabled = false;
                    this.btnSave.Enabled = false;
                }
                this.ddlLabelClass.SelectedValue = model.TID.ToString();
                this.txtContent.Value = model.Content;
                this.txtDescription.Text = model.Description;
                ViewState["ID"] = model.ID;
            }
            else
            {
                bindType(true);
            }
        }
        #endregion

        #region 保存信息
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbSave_Click(object sender, EventArgs e)
        {
            YXShop.BLL.Templates.Label bll = new YXShop.BLL.Templates.Label();
            YXShop.Model.Templates.Label model = new YXShop.Model.Templates.Label();
            YXShop.Model.Templates.Label IsExit = bll.GetModelName(ViewState["LabelName"].ToString());
            model.TID = int.Parse(this.ddlLabelClass.SelectedValue);
            model.Name = ViewState["LabelName"].ToString();
            model.Content = this.txtContent.Value;
            model.Description = this.txtDescription.Text;
            model.CreateTime = System.DateTime.Now;
            model.LabelClass = 1;
            model.Moduleid = 0;
            model.IsStoreLabel = 0;
            if (ViewState["ID"] != null)
            {
                model.ID = Convert.ToInt32(ViewState["ID"].ToString());
                if (IsExit != null)
                {
                    if (Convert.ToInt32(ViewState["ID"].ToString()) != IsExit.ID)
                    {
                        this.ltlMsg.Text = "操作失败，标签名不能重复，请重新输入标签名。";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionOk";
                        return;
                    }
                }
                if (bll.Amend(model) != 0)
                {
                    this.ltlMsg.Text = "操作成功，已保存该信息";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                }


            }
            else
            {
                if (IsExit != null)
                {
                    this.ltlMsg.Text = "操作失败，标签名不能重复，请重新输入标签名。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                    return;
                }
                if (bll.Create(model) != 0)
                {
                    this.ltlMsg.Text = "操作成功，已保存该信息";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                }
            }
        }
        #endregion

    }
}
